package org.xlp.tree;

import org.xlp.utils.collection.XLPCollectionUtil;

import java.util.ArrayList;
import java.util.List;

/**
 * Description: 简化树创建工具类
 * <br/>date: 2023/12/7 16:09
 *
 * @version 1.0
 * @author: xlp
 */
public class TreeCreatorUtils {
    /**
     * 更据给定的数据，创建出一颗树
     * @param source
     * @param <T>
     * @param <S>
     * @return 返回创建好的树信息，假如参数为空，则返回空集合
     */
    public static <T, S extends TreeNode<T, S>> List<S> treeCreate(List<S> source){
        if (XLPCollectionUtil.isEmpty(source)) return new ArrayList<>(0);
        //组成树
        TreeCreator<T, S> treeCreator = new TreeCreator<>();
        treeCreator.setTreeNodes(source);
        //创建树
        treeCreator.tree();
        return treeCreator.getRealTree();
    }
}
